package zgrjb.snbq.teach_platform.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;

/**
 * Redis缓存配置
 * 提升系统性能，减少数据库查询
 */
@Configuration
@EnableCaching
@Slf4j
public class RedisConfig {

    /**
     * 配置RedisTemplate
     * 设置序列化器，提高性能
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);

        // 设置Key的序列化器
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());

        // 设置Value的序列化器
        GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        template.setValueSerializer(jsonRedisSerializer);
        template.setHashValueSerializer(jsonRedisSerializer);

        template.afterPropertiesSet();
        log.info("RedisTemplate配置完成");
        return template;
    }

    /**
     * 配置缓存管理器
     * 设置不同类型数据的缓存策略
     */
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
        RedisCacheConfiguration defaultConfig = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofMinutes(30)) // 默认缓存30分钟
                .serializeKeysWith(org.springframework.data.redis.serializer.RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
                .serializeValuesWith(org.springframework.data.redis.serializer.RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));

        RedisCacheManager cacheManager = RedisCacheManager.builder(redisConnectionFactory)
                .cacheDefaults(defaultConfig)
                .transactionAware()
                // 配置不同类型的缓存策略
                .withCacheConfiguration("userInfo", defaultConfig.entryTtl(Duration.ofHours(2))) // 用户信息缓存2小时
                .withCacheConfiguration("classList", defaultConfig.entryTtl(Duration.ofMinutes(15))) // 班级列表缓存15分钟
                .withCacheConfiguration("courseList", defaultConfig.entryTtl(Duration.ofMinutes(15))) // 课程列表缓存15分钟
                .withCacheConfiguration("fileList", defaultConfig.entryTtl(Duration.ofMinutes(10))) // 文件列表缓存10分钟
                .withCacheConfiguration("examList", defaultConfig.entryTtl(Duration.ofMinutes(5))) // 考试列表缓存5分钟
                .build();

        log.info("Redis缓存管理器配置完成");
        return cacheManager;
    }
} 